package com.medical.admin.reserve.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.admin.hospital.mapper.HospitalMapper;
import com.medical.admin.patient.mapper.PatientMapper;
import com.medical.admin.reserve.common.dto.AppointmentRegisterRequest;
import com.medical.admin.reserve.common.dto.SubscribeDto;
import com.medical.admin.reserve.entity.Subscribe;
import com.medical.admin.reserve.mapper.SubscribeAllMapper;
import com.medical.admin.reserve.mapper.SubscribeMapper;
import com.medical.admin.reserve.service.SubscribeService;
import com.qf.medical.ex.ServiceException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/*
 * @Date : 2024/4/29 下午4:54
 * @Created by Lang
 * @ProjectName : medical-appointment-registration
 */
@Service
public class SubscribeServiceImpl implements SubscribeService {

    @Resource
    private SubscribeMapper subscribeMapper;
    @Resource
    private PatientMapper patientMapper;
    @Resource
    private HospitalMapper hospitalMapper;
    @Resource
    private SubscribeAllMapper subscribeAllMapper;

    @Override
    public Integer addSubscribe(SubscribeDto subscribeDto) {
        if (ObjectUtil.isEmpty(patientMapper.selectById(subscribeDto.getPatientId()))){
            throw new ServiceException(ResponseCode.PATIENT_NULL_ERROR);
        }
        if (ObjectUtil.isEmpty(hospitalMapper.selectById(subscribeDto.getHospitalId()))){
            throw new ServiceException(ResponseCode.HOSPITAL_NOT_EXIST_ERROR);
        }
        Subscribe subscribe = BeanConvertUtils.convertTo(subscribeDto, Subscribe::new);
        subscribe.setSubscribeId(IdUtil.getSnowflakeNextId());
        return subscribeMapper.insert(subscribe);
    }

    @Override
    public SubscribeDto detail(Long subscribeId) {
        return subscribeAllMapper.selectDetail(subscribeId);
    }

    @Override
    public Integer updateRemarkById(Long subscribeId, String remark) {
        Subscribe subscribe = new Subscribe();
        subscribe.setSubscribeId(subscribeId);
        subscribe.setSubscribeRemark(remark);
        return subscribeMapper.updateById(subscribe);
    }

    @Override
    public Integer delById(Long subscribeId) {
        Subscribe subscribe = new Subscribe();
        subscribe.setSubscribeId(subscribeId);
        subscribe.setStatus(0);
        return subscribeMapper.updateById(subscribe);
    }

    @Override
    public IPage<SubscribeDto> list(Integer pageNum, Integer pageSize, AppointmentRegisterRequest appointmentRegisterRequest) {
        Page<SubscribeDto> pageParam = new Page<>(pageNum, pageSize);
        return subscribeAllMapper.list(pageParam,appointmentRegisterRequest);
    }
}
